#include "mythread1.h"

#include <QtAlgorithms>

mythread1::mythread1(QObject *parent) : QObject(parent), QRunnable()
{
    setAutoDelete(true);
}

mythread1::~mythread1()
{
    qDebug() << "haha";
}

void mythread1::recvNum(int num)
{
    _num = num;
}

void mythread1::run()
{
    qDebug() << QThread::currentThread();
    QVector<int> v;
    for(int i = 0; i < _num; i++)
    {
        v.push_back(qrand() % 100000);
    }

    emit sendArray(v);
}



//----------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------


mythread2::mythread2(QObject *parent) : QObject(parent), QRunnable()
{
    setAutoDelete(true);
}



void mythread2::recvArray(QVector<int> v)
{
    _v = v;
}


void mythread2::run()
{
    qDebug() << QThread::currentThread();
    //冒泡排序
    int n = _v.size();
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < n - i - 1; j++)
        {
            if(_v[j] > _v[j + 1])
            {
                qSwap(_v[j], _v[j+1]);
            }

        }
    }

    emit sendArray(_v);
}




//----------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------



mythread3::mythread3(QObject *parent) : QObject(parent), QRunnable()
{
    setAutoDelete(true);
}



void mythread3::recvArray(QVector<int> v)
{
    _v = v;
}

void mythread3::InsertSort(QVector<int>& a,  int n)
{
    for (int i = 0; i < n - 1; ++i)
    {
        int end = i;
        // 单趟排序：[0, end]有序 end+1位置的值，插入进入，保持他依旧有序
        int tmp = a[end + 1];
        while (end >= 0)
        {
            if (tmp < a[end])
            {
                a[end + 1] = a[end];
                --end;
            }
            else
            {
                break;
            }
        }
        a[end + 1] = tmp;
    }
}


void mythread3::run()
{
    qDebug() << QThread::currentThread();
    //冒泡排序
    InsertSort(_v, _v.size());
    emit sendArray(_v);
}





